﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace XFPPT.NET.Models
{

    /// <summary>
    /// 根响应类，包含整个响应的结构信息
    /// Root response class, containing the entire response structure information
    /// </summary>
    public class CreateOutlineResponse
    {
        /// <summary>
        /// 响应标识，用于表示响应的状态
        /// Response flag, used to indicate the status of the response
        /// </summary>
        public bool? flag { get; set; }

        /// <summary>
        /// 错误码，用于标识响应中可能出现的错误类型
        /// Error code, used to identify the type of error that may occur in the response
        /// </summary>
        public long? code { get; set; }

        /// <summary>
        /// 错误详情，提供关于错误的详细描述
        /// Error details, providing a detailed description of the error
        /// </summary>
        public string? desc { get; set; }

        /// <summary>
        /// 不用关注，预留字段
        /// Don't pay attention, reserved field
        /// </summary>
        public object? count { get; set; }

        /// <summary>
        /// 包含请求大纲和相关数据的对象
        /// Object containing the request outline and related data
        /// </summary>
        public PPTCreateOutlineData? data { get; set; }

        /// <summary>
        /// 此字段目前为空，可预留扩展使用
        /// This field is currently null and can be reserved for future expansion
        /// </summary>
        public object? sid { get; set; }
        [JsonIgnore]
        private string realJsonstring = string.Empty;

        /// <summary>
        /// Gets or sets the real JSON string representation of the response.
        /// 获取或设置响应的真实 JSON 字符串表示。
        /// </summary>
        [JsonIgnore]
        public string? RealJsonstring
        {
            get { return realJsonstring; }
            set
            {
                realJsonstring = value!;
                try
                {
                    DeserializePPTCreateOutlineResponse(value!);
                }
                catch (Exception)
                {
                    // 可添加日志记录等操作
                }
            }
        }

        /// <summary>
        /// 反序列化PPT大纲响应的 JSON 字符串到当前对象。
        /// 此方法将传入的 JSON 字符串解析为对应的属性值，并赋值给当前对象的各个属性。
        /// </summary>
        /// <param name="json">待反序列化的 JSON 字符串。</param>
        public virtual void DeserializePPTCreateOutlineResponse(string json)
        {
            try
            {
                JsonNode? jsonNode = JsonNode.Parse(json);
                if (jsonNode != null)
                {
                    try
                    {
                        if (jsonNode["flag"] != null)
                        {
                            this.flag = Convert.ToBoolean(jsonNode["flag"]!.ToString());
                        }
                    }
                    catch { }
                    try
                    {
                        if (jsonNode["code"] != null)
                        {
                            this.code = Convert.ToInt64(jsonNode["code"]!.ToString());
                        }
                    }
                    catch { }
                    try
                    {
                        if (jsonNode["desc"] != null)
                        {
                            this.desc = jsonNode["desc"]!.ToString();
                        }
                    }
                    catch { }
                    try
                    {
                        if (jsonNode["count"] != null)
                        {
                            this.count = Convert.ToInt64(jsonNode["count"]!.ToString());
                        }
                    }
                    catch { }
                    try
                    {
                        if (jsonNode["sid"] != null)
                        {
                            this.sid = jsonNode["sid"]!.ToString();
                        }
                    }
                    catch { }

                    if (jsonNode["data"] != null)
                    {
                        this.data = new PPTCreateOutlineData();
                        var dataNode = jsonNode["data"]!;
                        try
                        {
                            if (dataNode["sid"] != null)
                            {
                                this.data.sid = dataNode["sid"]!.ToString();
                            }
                        }
                        catch { }

                        if (dataNode["outline"] != null)
                        {
                            this.data.outline = new PPTCreateOutlineOutline();
                            var outlineNode = dataNode["outline"]!;
                            try
                            {
                                if (outlineNode["title"] != null)
                                {
                                    this.data.outline.title = outlineNode["title"]!.ToString();
                                }
                            }
                            catch { }
                            try
                            {
                                if (outlineNode["subTitle"] != null)
                                {
                                    this.data.outline.subTitle = outlineNode["subTitle"]!.ToString();
                                }
                            }
                            catch { }

                            if (outlineNode["chapters"] != null)
                            {
                                this.data.outline.chapters = new List<PPTCreateOutlineChapter>();
                                foreach (var chapterNode in outlineNode["chapters"]!.AsArray())
                                {
                                    var chapter = new PPTCreateOutlineChapter();
                                    try
                                    {
                                        if (chapterNode!["chapterTitle"] != null)
                                        {
                                            chapter.chapterTitle = chapterNode["chapterTitle"]!.ToString();
                                        }
                                    }
                                    catch { }

                                    if (chapterNode!["chapterContents"] != null)
                                    {
                                        chapter.chapterContents = new List<PPTCreateOutlineChapterContent>();
                                        foreach (var contentNode in chapterNode["chapterContents"]!.AsArray())
                                        {
                                            var content = new PPTCreateOutlineChapterContent();
                                            try
                                            {
                                                if (contentNode!["chapterTitle"] != null)
                                                {
                                                    content.chapterTitle = contentNode["chapterTitle"]!.ToString();
                                                }
                                            }
                                            catch { }
                                            chapter.chapterContents.Add(content);
                                        }
                                    }
                                    this.data.outline.chapters.Add(chapter);
                                }
                            }
                        }
                    }
                }
            }
            catch { }
        }
    }

    /// <summary>
    /// 包含请求大纲唯一标识和大纲数据的类
    /// Class containing the unique identifier of the request outline and the outline data
    /// </summary>
    public class PPTCreateOutlineData
    {
        /// <summary>
        /// 请求大纲唯一id，后续通过大纲生成ppt可能需要
        /// Unique ID of the request outline, may be needed for generating PPT from the outline later
        /// </summary>
        public string? sid { get; set; }

        /// <summary>
        /// 大纲数据对象，包含 PPT 的标题、副标题和章节信息
        /// Outline data object, containing the title, subtitle and chapter information of the PPT
        /// </summary>
        public PPTCreateOutlineOutline? outline { get; set; }
    }

    /// <summary>
    /// 大纲数据类，包含 PPT 的主标题、副标题和章节列表
    /// Outline data class, containing the main title, subtitle and chapter list of the PPT
    /// </summary>
    public class PPTCreateOutlineOutline
    {
        /// <summary>
        /// PPT主标题
        /// Main title of the PPT
        /// </summary>
        public string? title { get; set; }

        /// <summary>
        /// PPT副标题
        /// Subtitle of the PPT
        /// </summary>
        public string? subTitle { get; set; }

        /// <summary>
        /// 章节列表，包含每个章节的标题和内容
        /// List of chapters, containing the title and content of each chapter
        /// </summary>
        public List<PPTCreateOutlineChapter>? chapters { get; set; }
    }

    /// <summary>
    /// 章节类，包含章节标题和二级大纲内容列表
    /// Chapter class, containing the chapter title and the list of secondary outline contents
    /// </summary>
    public class PPTCreateOutlineChapter
    {
        /// <summary>
        /// 章节、子章节标题名称
        /// Title name of the chapter or sub - chapter
        /// </summary>
        public string? chapterTitle { get; set; }

        /// <summary>
        /// 二级大纲内容列表
        /// List of secondary outline contents
        /// </summary>
        public List<PPTCreateOutlineChapterContent>? chapterContents { get; set; }
    }

    /// <summary>
    /// 二级大纲内容类，包含内容的标题
    /// Secondary outline content class, containing the title of the content
    /// </summary>
    public class PPTCreateOutlineChapterContent
    {
        /// <summary>
        /// 二级大纲内容的标题
        /// Title of the secondary outline content
        /// </summary>
        public string? chapterTitle { get; set; }
    }

}
